package com.ygl.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygl.entity.User;
import com.ygl.mapper.UserDao;
import com.ygl.utils.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ygl
 * @date 2021/7/7
 * @description xxx
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public Integer create(User user) {
        return userDao.create(user);
    }

    public Integer delete(Integer id) {
        return userDao.delete(Maps.build(id).getMap());
    }

    public Integer update(User user) {
        return userDao.update(Maps.build(user.getId()).beanToMapUpdate(user));
    }

    public PageInfo<User> query(User user) {
        if (user != null && user.getPage() != null) {
            PageHelper.startPage(user.getPage(), user.getSize());
        }
        List<User> list = userDao.query(Maps.build().beanToMap(user));
        return new PageInfo<User>(list);
    }

    public User detail(Integer id) {
        return userDao.detail(Maps.build(id).getMap());
    }

    public User login(String userName,String password) {
        System.out.println(Maps.build().put("userName",userName).put("password",password).getMap());
        return userDao.login(Maps.build().put("userName",userName).put("password",password).getMap());
    }

    public Integer count(User user) {
        return userDao.count(Maps.build().beanToMap(user));
    }
}
